package model

import "gorm.io/gorm"

type User struct {
	gorm.Model
	Phone    string `gorm:"type:char(11)" json:"phone"`
	QQ       string `gorm:"type:varchar(12)" json:"QQ"`
	NickName string `gorm:"type:varchar(20)" json:"nickName"`
	Password string `gorm:"type:char(32)" json:"password"`
}

func (u *User) IsQQ(db *gorm.DB, QQ string) error {
	err := db.Where("qq = ?", QQ).First(&u).Error
	return err
}

func (u *User) IsPhone(db *gorm.DB, phone string) error {
	err := db.Where("phone = ?", phone).First(&u).Error
	return err
}

func (u *User) Register(db *gorm.DB) error {
	err := db.Create(&u).Error
	return err
}

func (u *User) SearchUser(db *gorm.DB, keyword string) error {
	err := db.Where("qq like ? or nick_name like ?", "%"+keyword+"%", "%"+keyword+"%").Find(&u).Error
	return err
}
